﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TeamCitySharp;
using Teamboard.Properties;
using Teamboard.Models;
using Teamboard.Helpers;

namespace Teamboard.TeamCity
{
    public class TeamCityHelper
    {
        private ITeamCityClient _clnt;
        private SettingsManager _settings;

        public SettingsManager SettingsManager
        {
            get
            {
                if (_settings == null)
                {
                    _settings = new SettingsManager();
                }

                return _settings;
            }
            set
            {
                _settings = value;
            }
        }
        public ITeamCityClient Client { 
            get 
            {
                if (_clnt == null)
                {
                    _clnt = new TeamCityClient(Settings.Default.BuildServerName, Settings.Default.UseSSL);
                    Connect();
                }
                return _clnt;
            }
            set
            {
                _clnt = value;
            }
        }

        private void Connect()
        {
            if (Settings.Default.LoginAsGuest)
            {
                Client.Connect(string.Empty, string.Empty, Settings.Default.LoginAsGuest);
            }
            else
            {
                Client.Connect(Settings.Default.UserName, Settings.Default.Password);
            }
        }

        public IEnumerable<Project> GetDashBoardData()
        {
            var projects = new List<Project>();

            foreach (var projectToGet in SettingsManager.GetProjectsList())
            {
                projects.Add(GetProject(projectToGet));
            }


            return projects;
        }


        private Project GetProject(string projectToGet)
        {
            try
            {
                var proj = Client.ProjectByName(projectToGet);
                Project project = proj.Map<TeamCitySharp.DomainEntities.Project, Project>();

                var configNames = SettingsManager.GetProjectConfigurations(projectToGet);
                foreach (var configName in configNames)
                {
                    var config = GetConfiguration(projectToGet, configName);
                    if (config != null)
                    {
                        project.Configurations.Add(config);
                    }
                }

                return project;
            }
            catch { }

            return null;
        }

        private BuildConfiguration GetConfiguration(string projectName, string configName)
        {
            try
            {
                var conf = Client.BuildConfigByProjectNameAndConfigurationName(projectName, configName);
                var tcLastBuild = Client.LastBuildByBuildConfigId(conf.Id);
                var lastSuccessfullBuild = Client.LastSuccessfulBuildByBuildConfigId(conf.Id);
                var configuration = conf.Map<TeamCitySharp.DomainEntities.BuildConfig, BuildConfiguration>();

                configuration.LastSuccessfulBuild = lastSuccessfullBuild.Number;
                configuration.Status = tcLastBuild.Status;
                configuration.LatestBuildNumbers.Add(tcLastBuild.Number);

                return configuration;
            }
            catch
            {
            }

            return null;
        }

        public IEnumerable<Project> GetProjectsAndConfigurations()
        {
            var projects = new List<Project>();
            var tcProjects = Client.AllProjects();
            if (tcProjects != null)
            {
                foreach (var p in tcProjects)
                {
                    var project = p.Map<TeamCitySharp.DomainEntities.Project, Project>();
                    var confs = Client.BuildConfigsByProjectId(p.Id);
                    if (confs != null)
                    {
                        foreach (var conf in confs)
                        {
                            project.Configurations.Add(conf.Map<TeamCitySharp.DomainEntities.BuildConfig, BuildConfiguration>());
                        }
                    }

                    projects.Add(project);
                }
            }

            return projects;
            
        }
    }
}